package source;

import source.view.MainScreen;
import source.view.ViewData;
import source.controller.*;
import source.events.*;
import source.model.DataHandler;
import source.model.utility.MapGenerator;
import source.model.Player;
import source.model.Position;
import source.model.type.UnitType;
import source.model.Unit;

/**
 * The Driver is the entry point into the game application.  Driver initializes the
 * game model/view/controller and assembles the components together.  After initialization
 * Turn.nextTurn() is called to start the game.
 * @author Joe
 *
 */
public class Driver {
	static class Refresher extends Thread{
		private MainScreen ms;
		public Refresher(MainScreen ms){
			this.ms = ms;
		}
		
		public void run(){
			try{
				while(!interrupted()){
					Thread.sleep(50);
					ms.updateScreens();
					ms.repaint();
				}
			}
			catch(InterruptedException e){}
		}
	}
	
	public static final int numPlayers = 2;
	private static final int startingOre = 100000000;
	public static void main(String args[])
	{
		
		//initilize view event handling
		EventHandler<ViewEvent> veh = new EventHandler<ViewEvent>();
		
		// initilize the data handler
		source.model.Position.setBounds(10, 10);
		DataHandler dh = new DataHandler(veh);
		
		ViewData viewData = new ViewData(dh);
		ViewEventListener vel = new ViewEventListener(viewData);
		veh.registerListener(vel);
		
		/**
		 * initialize event handlers
		**/
		EventHandler<ModelEvent> meh = new EventHandler<ModelEvent>();
		ModelEventListener mel = new ModelEventListener(dh);
		meh.registerListener(mel);
		
		/**initialize players
		*initialize player key listeners and userinputmanagers
		*initialize players' units
		*give unit id's to pertinent players
		**/
		
		PlayerKeyListener pkl[] = new PlayerKeyListener[numPlayers];
		
		Position[] tn = new Position[2];
		tn[0] = new Position(0, 0);
		tn[1] = new Position(9, 9);
		UserInputManager uim = new UserInputManager(0, meh);
		for(int i = 0; i < numPlayers; i++)
		{

			pkl[i] = CustomKeyFileManager.createPlayerKeyListener(i);
			pkl[i].setUserInputManager(uim);
			Player p = new Player(i, startingOre);
			dh.addPlayer(p);
			Unit unit1 = new Unit( UnitType.COLONIST ); 
			dh.addGameObject(unit1, tn[i], i);
			Unit unit2 = new Unit( UnitType.EXPLORER ); 
			dh.addGameObject(unit2, tn[i], i);		
			Unit unit3 = new Unit( UnitType.EXPLORER ); 
			dh.addGameObject(unit3, tn[i], i);	
		}
		//TODO:  Change so it loops through multiple key listners
		//pkl[1].setActiveFlag(false);
		
		/**initialize turn
		**/
		Turn t = new Turn(veh, meh);
		meh.registerListener(t);
		
		/**initialize gui
		 * 
		**/
		MainScreen m = new MainScreen(pkl, viewData);
		m.setFocusTraversalKeysEnabled(false);
		//TODO:  Change so it loops through multiple key listeners
		m.addKeyListener(pkl[0]);
		m.addKeyListener(pkl[1]);
		m.setFocusable(true);
		m.setVisible(true);
	
		vel.setMainScreen(m);
		
		// initialize map
		MapGenerator.createDefaultMap(dh);
		
		Thread refresh = new Refresher(m);
		refresh.start();
		
		/**send event to redraw gui
		*send a nextturn event for the first player and its entities to call nextturn
		**/

		meh.fireEventNow(new ModelEvent(0, ModelEventType.NEXTTURN));
		

	}
}